package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.provider.api.model.dto.GoodPrintRpcDto;
import com.weixing.mall.provider.api.model.vo.ActivityProductVo;
import com.weixing.mall.provider.mapper.*;
import com.weixing.mall.provider.model.domain.*;
import com.weixing.mall.provider.model.dto.GoodsPrintDto;
import com.weixing.mall.provider.model.query.GoodsQuery;
import com.weixing.mall.provider.model.vo.GoodsVo;
import com.weixing.mall.provider.service.IGoodsPrintService;
import com.weixing.mall.util.CommodityCode;
import com.weixing.mall.util.CommUtil;
import com.weixing.mall.util.ListUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author wx
 * @Date 2019/8/9 10:52
 * @Version 1.0
 */

/**
 * <p>
 * 商品模块-商品表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-20
 */
@Service
public class GoodsPrintServiceImpl extends ServiceImpl<GoodsPrintMapper, GoodsPrint> implements IGoodsPrintService {

    @Autowired
    private GoodsPrintMapper goodsPrintMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void codePrint(Long[] ids, SysAuth auth) {
        List<GoodsPrint> insertBathList = Lists.newArrayList();
        List<ActivityProductVo> goodslist = productMapper.findActProductVoByIds(ids);
        goodsPrintMapper.delete(new QueryWrapper<GoodsPrint>().eq("created_by",auth.getUid()));
       for(Long t1:ids) {
           for (int i=0;i<ids.length;i++){
               ActivityProductVo goods=getVo(ids[i],goodslist);

               if (t1.longValue() == goods.getProductId().longValue()) {
                   GoodsQuery query = new GoodsQuery();
                   query.setGoodsSn(goods.getGoodsSn());
                   List<GoodsVo> goods1 = goodsMapper.selectList(query);
                   if (goods1.size() == 0) {
                       continue;
                   }
                   Goods goods2 = new Goods();
                   goods2.setPrintCount(goods1.get(0).getPrintCount() == null ? 1 : goods1.get(0).getPrintCount() + 1);
                   goods2.setId(goods1.get(0).getId());
                   goodsMapper.updateById(goods2);
                   GoodsPrint entity = new GoodsPrint();
                   BeanUtils.copyProperties(goods, entity);
                   entity.setCommodityCode(CommodityCode.creatCode() + CommUtil.uuid(6, goods.getId()));
                   entity.setStyleNo(goods1.get(0).getStyleNo());
                   entity.setProductSn(goods.getProductSn());
                   entity.setCatName(goods.getCatName());
                   entity.setCreatedBy(auth.getUid());
                   entity.setMarketPrice(goods1.get(0).getMarketPrice());
                   insertBathList.add(entity);
                   break;

               }
           }
       }
        saveBatch(insertBathList,ListUtil.SIZE);
    }

    private ActivityProductVo getVo(Long id, List<ActivityProductVo> goodslist) {
        for (ActivityProductVo activityProductVo : goodslist) {
            if (activityProductVo.getProductId().longValue() == id.longValue()) {
                return activityProductVo;
            }
        }
        return null;
    }

    @Override
    public String findCommodityCodeByGoodsSn(String goodsSn) {
        if(StrUtil.isEmpty(goodsSn)){
            return null;
        }
        GoodsPrint goodsPrint = getOne(new LambdaQueryWrapper<GoodsPrint>().eq(GoodsPrint::getGoodsSn,goodsSn).last("LIMIT 1"));
        return ObjectUtil.isNotEmpty(goodsPrint)?goodsPrint.getCommodityCode():null;
    }

    @Override
    public void codePrint(List<GoodsPrintDto> goodPrintList,SysAuth auth) {
       Long [] ids=new Long[goodPrintList.size()];
       for(int i=0;i<goodPrintList.size();i++){
            ids[i]=goodPrintList.get(i).getProductId();
        }
        List<GoodsPrint> insertBathList = Lists.newArrayList();
        List<ActivityProductVo> goodslist = productMapper.findActProductVoByIds(ids);
        goodsPrintMapper.delete(new QueryWrapper<GoodsPrint>().eq("created_by",auth.getUid()));
        for(GoodsPrintDto dto:goodPrintList) {
            for (ActivityProductVo goods : goodslist) {
                Long  proudctId=goods.getId();
                if (proudctId.longValue() == dto.getProductId().longValue()) {
                    Long number = dto.getNumber();
                    for (int i = 0; i < number; i++) {
                        GoodsQuery query = new GoodsQuery();
                        query.setGoodsSn(goods.getGoodsSn());
                        List<GoodsVo> goods1 = goodsMapper.selectList(query);
                        if (goods1.size() == 0) {
                            continue;
                        }
                        Goods goods2 = new Goods();
                        goods2.setPrintCount(goods1.get(0).getPrintCount() == null ? 1 : goods1.get(0).getPrintCount() + 1);
                        goods2.setId(goods1.get(0).getId());
                        goodsMapper.updateById(goods2);
                        GoodsPrint entity = new GoodsPrint();
                        BeanUtils.copyProperties(goods, entity);
                        entity.setCommodityCode(CommodityCode.creatCode() + CommUtil.uuid(6, goods.getId()));
                        entity.setStyleNo(goods1.get(0).getStyleNo());
                        entity.setProductSn(goods.getProductSn());
                        entity.setCatName(goods.getCatName());
                        entity.setCreatedBy(auth.getUid());
                        entity.setMarketPrice(goods1.get(0).getMarketPrice());
                        insertBathList.add(entity);

                    }
                }
            }
        }
        saveBatch(insertBathList,ListUtil.SIZE);

    }

    @Override
    public void goodPrint(List<GoodPrintRpcDto> goodPrintRpcDtoList) {
        List<GoodsPrint> insertBathList = Lists.newArrayList();
        if(goodPrintRpcDtoList.size()>0){
            goodsPrintMapper.delete(new QueryWrapper<GoodsPrint>().eq("created_by",goodPrintRpcDtoList.get(0).getCreatedBy()));
        }
          for(GoodPrintRpcDto goodPrintRpcDto:goodPrintRpcDtoList){
              GoodsPrint entity = new GoodsPrint();
              BeanUtils.copyProperties(goodPrintRpcDto, entity);
              entity.setCommodityCode(CommodityCode.creatCode() + CommUtil.uuid(6, goodPrintRpcDto.getId()));
              insertBathList.add(entity);
          }
        saveBatch(insertBathList, ListUtil.SIZE);
    }
}
